package com.zjm.creational.simple_factory.pizzastore.order;

import com.zjm.creational.simple_factory.pizzastore.pizza.CheesePizza;
import com.zjm.creational.simple_factory.pizzastore.pizza.GreekPizza;
import com.zjm.creational.simple_factory.pizzastore.pizza.PepperPizza;
import com.zjm.creational.simple_factory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OrderPizza {

    // 方法一
    public OrderPizza() {
        Pizza pizza = null;
        String orderType;
        do {
            orderType = getType();
            if ("greek".equals(orderType)) {
                pizza = new GreekPizza();
                pizza.setName("Greek");
            } else if ("cheese".equals(orderType)) {
                pizza = new CheesePizza();
                pizza.setName("Cheese");
            } else if ("pepper".equals(orderType)) {
                pizza = new PepperPizza();
                pizza.setName("Pepper");
            } else {
                break;
            }
            // 披萨制作过程
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();

        } while (true);
    }

    // 方法二：简单工厂模式
    SimpleFactory simpleFactory;
    Pizza pizza = null;

    // 构造器
    public OrderPizza(SimpleFactory simpleFactory) {
        setSimpleFactory(simpleFactory);
    }

    public void setSimpleFactory(SimpleFactory simpleFactory) {
        String orderType = "";
        this.simpleFactory = simpleFactory;
        do {
            orderType = getType();
            pizza = this.simpleFactory.createPizza(orderType);

            if (pizza != null) {
                // 披萨制作过程
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("订购披萨失败");
                break;
            }
        } while (true);
    }

    private String getType() {

        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入披萨的类型：");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
